<template>
  <div class="moreSelete">
    <el-input :placeholder="checkedLists.length?`${checkedListNames}`:'请输入内容'" v-model="inputVal" @input="filterVal"
              :class="checkedLists.length?`color-black`:''">
      <el-popover slot="append" placement="bottom-end" width="200" trigger="click" v-model="visible">
        <el-checkbox v-model="checkAll" @change="handleCheckAll">全选 (已选{{checkedLists.length}})</el-checkbox>
        <div style="max-height: 320px;overflow: scroll" class="moreSeletePup">
          <el-checkbox-group v-model="checkedLists" @change="CheckChange">
            <el-checkbox v-for="item in MoreList" :label="item" :key="item.id">{{item[showName]}}</el-checkbox>
          </el-checkbox-group>
        </div>
        <el-button slot="reference"><i class="el-icon-arrow-down"></i>请选择</el-button>
      </el-popover>
    </el-input>
  </div>
</template>

<script>
  export default {
    name: 'moresele',
    data(){
      return {
        inputVal: '',
        visible: false,
        checkAll: false,
        checkedLists: [],
        checkedListNames: [],
        inputIds: [],
      }
    },
    props: {
      dataList: {
        type: Array
      },
      showName: {
        type: String
      }
    },
    computed: {
      MoreList(){
        return this.dataList.filter(item=>{
          return item[this.showName].indexOf(this.inputVal) > -1
        })
      }
    },
    methods: {
      handleCheckAll(val){
        this.checkedLists = val?[...new Set([...this.checkedLists, ...this.MoreList])]:[]
        this.checkedListNames = []
        this.inputIds = []
        this.checkedLists.map(item=>{
          this.checkedListNames.push(item[this.showName])
          this.inputIds.push(item.id.toString())
        })
        this.$emit('input', this.inputIds)
      },
      CheckChange(){
        this.checkedListNames = []
        this.inputIds = []
        this.checkedLists.map(item=>{
          this.checkedListNames.push(item[this.showName])
          this.inputIds.push(item.id.toString())
        })
        this.$emit('input', this.inputIds)
      },
      filterVal(){
        this.visible = true
      }
    },
    watch: {
      visible() {
        if(!this.visible){
          this.inputVal = ''
        }
      }
    }
  }
</script>

<style lang="scss">
  .el-popover{
    .moreSeletePup{
      padding-bottom: 10px;
      .el-checkbox-group{
        label.el-checkbox{
          margin-left: 0;
          width: 100%;
          line-height: 24px;
        }
      }
    }
  }
</style>
